package com.neusoft.chenhanhua.d0915;

import java.awt.Graphics;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.HashSet;
import java.util.Iterator;

import javax.swing.JPanel;

public class CanvasPanel extends JPanel{
     private String type="l";
//     private Line line;
//     private Square square;
     private Shape shape;
     private HashSet  set;
     //private ShapeArray shapeArray;
	public void setType(String type) {
		this.type=type;
		
	}
	 public CanvasPanel() {
		 //添加点击事件
		 //MouseAdapter属于设配器开发模式
//		 addMouseListener(new MouseAdapter() {
//		});
		 System.out.println(type);
//		 if ("l".equals(type)) {
//			 line =new Line();
//				
//			}
		 addMouseListener(ma);
		 addMouseMotionListener(ma);
		 //shapeArray=new ShapeArray();
		 set=new HashSet();
	}
//	 private int x1;
//	 private int y1;
//	 private int x2;
//	 private int y2;
//	 private int w;
//	 private int h;
	 private MouseAdapter ma=new MouseAdapter() {
		 //鼠标事件可以让您跟踪鼠标按下，释放，点击，移动，拖动，进入组件，退出时和鼠标滚轮移动时
		 //我的理解主要是跟踪鼠标事件。。。。。
		 //鼠标点击时
		 public void mousePressed(MouseEvent e) {
				System.out.println("mousePressed");
				//获取点击坐标
//				x1 = e.getX();
//				y1 = e.getY();
//				System.out.println(type);
				if ("l".equals(type)) {
//					line.setX1(x1);
//					line.setY1(y1);
					shape=new Line();
					}else if("s".equals(type)) {
//						square.setX1(x1);
//						square.setY1(y1);
						shape =new Square();
					}
				shape.setX1(e.getX());
				shape.setY1(e.getY());
			}
		 //鼠标松开时调用
			public void mouseReleased(MouseEvent e) {
				System.out.println("mouseReleased");
//				x2 = e.getX();
//				y2 = e.getY();
//				if ("l".equals(type)) {
//					line.setX2(x2);
//					line.setY2(y2);
//					}else if("s".equals(type)) {
//						square.setX2(x2);
//						square.setY2(y2);
//					}
				shape.setX2(e.getX());
				shape.setY2(e.getY());
				//shapeArray.push(shape);
				set.add(shape);
				shape = null;
				repaint();
			}
			//鼠标拖动时调用
			public void mouseDragged(MouseEvent e) {
//				System.out.println("mouseDragged");
//				x2 = e.getX();
//				y2 = e.getY();
//				
//				if ("l".equals(type)) {
//					line.setX2(x2);
//					line.setY2(y2);
//					}else if("s".equals(type)) {
//						x2=x2-x1;
//						y2=y2-y1;
//						square.setX2(x2);
//						square.setY2(y2);
//					}
//				
				shape.setX2(e.getX());
				shape.setY2(e.getY());
				repaint();
			}
	};
	@Override
	public void paint(Graphics g) {
		System.out.println("paint执行了");
		super.paint(g);
		//把"l".equals()，"l"放前面解决type可能空值的情况
//		if("l".equals(type)) {
//			//画直线
//			//g.drawLine(x1, y1, x2, y2);
//			line.draw(g);
//		}else if("s".equals(type)) {
//			//画矩形
//			square.draw(g);
//			//g.drawRect(x1, y1, w, h);
//		}else if("c".equals(type)) {
//			//画矩形
//			g.drawOval(x1, y1, w, h);
//		}
		if(shape != null) {
			shape.draw(g);
		}
		Iterator iterator = set.iterator();
		while(iterator.hasNext()) {
			Shape x=(Shape)iterator.next();
			x.draw(g);
		}
		
		//shapeArray.draw(g);
	}
	

}
